package com.park.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.park.model.BillingInformation;
import com.park.model.Data;
import com.park.model.Invoice;
import com.park.model.InvoiceHistory;
import com.park.model.InvoiceUser;
import com.park.model.Paging;
import com.park.model.Root;
import com.park.model.SellerInformation;
import com.park.service.BillingInformationService;
import com.park.service.InvoiceHistoryService;
import com.park.service.InvoiceService;
import com.park.service.InvoiceUserService;
import com.park.service.SellerInformationService;
import com.park.service.Utility;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("invoice")
public class InvoiceController {
	@Autowired
	InvoiceService invoiceService;
	@Autowired
	private InvoiceHistoryService invoiceHistoryService;
	@Autowired
	private SellerInformationService sellerInformationService;
	@Autowired
	private BillingInformationService billingInformationService;
	@Autowired
	private InvoiceUserService invoiceUserService;
	private static Log logger = LogFactory.getLog(InvoiceController.class);
	
	@RequestMapping(value="putJedisTocken",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String putJedisTocken(){
		Jedis jedis = new Jedis("106.15.231.85", 6390);
		jedis.auth("liushuijiwusi");
		String accessToken=invoiceService.getTocken();
		jedis.set("invoiceAccessToken", accessToken);
		jedis.expire("invoiceAccessToken", 5000);
		return Utility.createJsonMsg(1001, accessToken);
	}
	@RequestMapping(value="getInvoiceInfo",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getInvoiceInfo(@RequestBody Map<String, Object> args){
		logger.info(args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String userId = (String)args.get("userId");
		String startTime = args.get("startTime") == null ? null : (String)args.get("startTime");
		String endTime = args.get("endTime") == null ? null : (String)args.get("endTime");
		int startPage = (int)args.get("startPage");
		int pageSize = (int)args.get("pageSize");
		List<InvoiceHistory> data = invoiceHistoryService.getInvoiceInfo(userId,startTime,endTime,(startPage-1)*pageSize,pageSize);
		int allCount = invoiceHistoryService.getInvoiceInfoTotal(userId,startTime,endTime);
		Paging page = new Paging(startPage, pageSize, allCount, data);
		resultMap.put("status", 1001);
		resultMap.put("body", page);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
	
	@RequestMapping(value="getSellerInformation",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getSellerInformation(@RequestBody Map<String, Object> args){
		logger.info(args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		int parkId = (int)args.get("parkId");
		SellerInformation sellerInformation = sellerInformationService.getInfo(parkId);
		resultMap.put("status", 1001);
		resultMap.put("body", sellerInformation);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
	@RequestMapping(value="getBillingInformation",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getBillingInformation(@RequestBody Map<String, Object> args){
		logger.info(args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String userId = (String)args.get("userId");
		List<BillingInformation> billingInformations = billingInformationService.getInfo(userId);
		resultMap.put("status", 1001);
		resultMap.put("body", billingInformations);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
	
	@RequestMapping(value="registerUser",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String registerUser(@RequestBody InvoiceUser invoiceUser){
		logger.info(invoiceUser);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		InvoiceUser user = invoiceUserService.getInfo(invoiceUser.getPhone(),invoiceUser.getOpenid(),null);
		if(user == null) {
			String userId = UUID.randomUUID().toString().replace("-", "").toLowerCase();
			invoiceUser.setUserId(userId);
			System.out.println(invoiceUser);
			int count = invoiceUserService.insert(invoiceUser);
			resultMap.put("status", 1001);
			resultMap.put("userId", userId);
			resultMap.put("message", "注册成功");
		}else {
			resultMap.put("status", 1001);
			resultMap.put("userId", user.getUserId());
			resultMap.put("message", "该用户已经注册");
		}
		return Utility.gson.toJson(resultMap);
	}
	@RequestMapping(value="getUserInfo",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getUserInfo(@RequestBody Map<String, String> args){
		logger.info(args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String phone = args.get("phone");
		String openId = args.get("openid");
		String userId = args.get("userId");
		InvoiceUser invoiceUser = invoiceUserService.getInfo(phone,openId,userId);
		resultMap.put("status", 1001);
		resultMap.put("body", invoiceUser);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
	@RequestMapping(value="updateUserByUserId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String updateUserByUserId(@RequestBody Map<String, String> args){
		logger.info(args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String userId = args.get("userId");
		String phone = args.get("phone");
		String openId = args.get("openid");
		int count = invoiceUserService.updateByUserId(userId,phone,openId);
		resultMap.put("status", 1001);
		resultMap.put("message", "修改成功");
		return Utility.gson.toJson(resultMap);
	}
}
